
<template>
  <div class="product-container">
    <!-- 左侧分类树 -->
    <div class="category-tree">
      <el-card shadow="never" class="tree-card">
        <div class="tree-header">
          <span>产品分类</span>
          <el-button type="text" icon="el-icon-plus" @click="onAddCategory">新增分类</el-button>
        </div>
        <el-tree
          ref="categoryTree"
          :data="categoryData"
          node-key="id"
          :props="defaultProps"
          :current-node-key="currentCategory ? currentCategory.id.toString() : ''"
          highlight-current
          @node-click="handleNodeClick"
          default-expand-all
        >
          <span class="custom-tree-node" slot-scope="{ node, data }">
            <span>{{ node.label }}</span>
            <span class="tree-actions">
              <el-button type="text" size="mini" @click.stop="onEditCategory(data)">
                <i class="el-icon-edit"></i>
              </el-button>
              <el-button type="text" size="mini" style="color: #F56C6C" @click.stop="onDeleteCategory(data)">
                <i class="el-icon-delete"></i>
              </el-button>
            </span>
          </span>
        </el-tree>
      </el-card>
    </div>

    <!-- 右侧产品列表 -->
    <div class="product-content">
      <SearchForm
        @search="onSearch"
        @reset="onReset"
        :search-list="searchList"
        :search-params="searchParams">
      </SearchForm>
      <div class="table-layout">
        <el-card shadow="never">
          <div class="table-handle">
            <el-button type="primary" size="mini" icon="el-icon-plus" @click="onAdd">新增产品</el-button>
          </div>
          <div class="table-box">
            <el-table
              size="large"
              stripe
              border
              v-loading="loading"
              max-height="500px"
              :data="tableData"
              row-key="id"
              element-loading-spinner="el-icon-loading"
              element-loading-background="rgba(255, 255, 255, 0.8)"
              @selection-change="onSelect"
            >
              <el-table-column type="selection" width="55"/>
              <el-table-column prop="id" label="编号"/>
              <el-table-column prop="name" label="名称"/>
              <el-table-column prop="category.name" label="分类"/>
              <el-table-column prop="post_url" label="封面">
                <template slot-scope="scope">
                  <el-image style="height: 60px" v-if="scope.row.post_url" :src="scope.row.post_url" fit="contain"></el-image>
                </template>
              </el-table-column>
              <el-table-column prop="sort" label="排序"/>
              <el-table-column prop="status" label="状态">
                <template slot-scope="scope">
                  <el-tag type="success" v-if="scope.row.status === 1">启用</el-tag>
                  <el-tag type="info" v-else>禁用</el-tag>
                </template>
              </el-table-column>
              <el-table-column label="操作" fixed="right" width="150px">
                <template slot-scope="scope">
                  <el-button type="text" @click="onEdit(scope.row)">编辑</el-button>
                  <el-button type="text" style="color: #F82222" @click="onDelete(scope.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-card>

        <div class="page-box">
          <el-pagination
            background
            :current-page="pageNo"
            :page-sizes="pageSizes"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalSize"
            @size-change="changePageSize"
            @current-change="changePageNo"
          />
        </div>
      </div>
    </div>

    <!-- 产品表单弹窗 -->
    <Add ref="addFormRef" @get-page="initData" />

    <!-- 分类表单弹窗 -->
    <CategoryForm ref="categoryFormRef" @refresh="getCategoryList" />
  </div>
</template>

<script>
import crud from "@/mixins/crud";
import { http } from "@/api";
import Add from "./add";
import CategoryForm from "./category-form";

export default {
  name: "index",
  components: {
    Add,
    CategoryForm
  },
  mixins: [crud],
  data() {
    return {
      request: http.product,
      categoryData: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      currentCategory: null,
      searchList: [
        {
          label: '编号',
          prop: 'id',
          name: 'input',
          clearable: true
        },
        {
          label: '名称',
          prop: 'name',
          name: 'input',
          clearable: true
        },
      ],
      searchParams: {
        name: '',
        category_id: ''
      },
      tableData: [],
      manual: true
    }
  },
  created() {
    this.getCategoryList()
  },
  methods: {
    createGroup() {
      http.tiia.createGroup()
    },
    // 获取分类列表
    async getCategoryList() {
      try {
        const res = await http.category.list()
        this.categoryData = res.data
        if (this.categoryData.length) {
          // 默认选中第一个分类
          this.currentCategory = this.categoryData[0]
          this.searchParams.category_id = this.currentCategory.id
          // 设置当前选中节点
          this.$nextTick(() => {
            if (this.$refs.categoryTree) {
              this.$refs.categoryTree.setCurrentKey(this.currentCategory.id)
            }
          })
          // 初始化加载数据
          this.initData()
        }
      } catch (error) {
        console.error('获取分类列表失败:', error)
      }
    },
    // 点击分类节点
    handleNodeClick(data) {
      this.currentCategory = data
      this.searchParams.category_id = data.id
      // 设置当前选中节点
      this.$refs.categoryTree.setCurrentKey(data.id)
      this.initData()
    },
    // 新增分类
    onAddCategory() {
      this.$refs.categoryFormRef.open()
    },
    // 编辑分类
    onEditCategory(data) {
      this.$refs.categoryFormRef.open(data)
    },
    // 删除分类
    onDeleteCategory(data) {
      this.$confirm('确认删除该分类吗？', '提示', {
        type: 'warning'
      }).then(() => {
        http.category.delete(data.id).then(() => {
          this.$message.success('删除成功')
          this.getCategoryList()
          this.onSearch()
        })
      })
    }
  }
}
</script>

<style scoped lang="scss">
.product-container {
  display: flex;
  height: 100%;
  gap: 20px;

  .category-tree {
    width: 280px;
    .tree-card {
      height: 100%;
    }
    .tree-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 15px;
    }
    .custom-tree-node {
      flex: 1;
      display: flex;
      align-items: center;
      justify-content: space-between;
      font-size: 14px;
      padding-right: 8px;
    }
    .tree-actions {
      opacity: 0;
      transition: opacity 0.2s;
    }
    .el-tree-node__content:hover .tree-actions {
      opacity: 1;
    }
  }

  .product-content {
    flex: 1;
    overflow: hidden;
  }
}
</style>
